package com.aglhz.nature.presenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;

import com.aglhz.nature.events.PersonDataViewEvent;
import com.aglhz.nature.modle.WXUserInfo;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.PersonDataView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class PersonDataPresenter extends BasePresenter{
	private String Refresh_token;
	private String openid;
	private String access_token;
	private String unionid;
	private String nickname;
	private String headimgurl;
	
	private static String TAG = PersonDataPresenter.class.getName();
	// view
		private PersonDataView personDataView;
	//data	
		private WXUserInfo wxUserInfo;
		
		

		
	public PersonDataPresenter(PersonDataView view) {
		super();
		personDataView = view;
	}



		public void loadInfo() {
			Log.e("loadWXInfo---------------", "loadWXInfo==================");
			this.getInfo();
		}
		
		
		
		
		
		
		
		
		
		// /*********************view operation*******************///
		@Subscribe(threadMode = ThreadMode.MAIN)
		public void onEventPersondataThread(PersonDataViewEvent event) {

			String msg = event.getMessage();
			Log.i(TAG, "PersonDataViewEvent:" + msg);
			// update UI By event
			if (PersonDataViewEvent.EVENT_UPDATE_USERINFO_VIEW == msg) {
				// update scroll view
				this.setUserView();
			} 
			
		}	
		
		public void setUserView(){
			personDataView.setHeadimage(wxUserInfo.getData()
				.getMemberInfo().getFace());
			personDataView.setUserName(wxUserInfo.getData()
				.getMemberInfo().getName());
		}
		
		
		
		
		
		
		
		
		
		public void getInfo(){
			Refresh_token = personDataView.setRefresh_token();
			openid = personDataView.setOpenid();
			access_token = personDataView.setAccess_token();
			unionid = personDataView.setUnionid();
			nickname = personDataView.setNickname();
			headimgurl = personDataView.setHeadimgurl();
			
			String url = "http://h5.aglhz.com/gc_community/client/login2.do?newAuth=true&token="
					+ access_token
					+ "&userId="
					+ openid
					+ "&userName="
					+ nickname
					+ "&headURL="
					+ headimgurl
					+ "&type=wxsession&unionid="
					+ unionid;
			Log.e("-------private void getUserInfo() {;_________________","---syyyy-----url---------------"+url);
			AsyncHttpClient client = new AsyncHttpClient();
			client.post(url, new TextHttpResponseHandler() {

				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					try {
						Log.e(TAG, "response" + responseString);

						Gson gson = new Gson();
						
						WXUserInfo UserInfo = gson.fromJson(responseString,
								WXUserInfo.class);
						wxUserInfo = (WXUserInfo) UserInfo.clone();
						Log.e("-------private void getUserInfo() {;_________________","--------url---------------"+wxUserInfo);
						int infocode = Integer.parseInt(wxUserInfo.getOther()
								.getCode());
						if (infocode == 200) {
							Log.e("-------private void getUserInfo() {;_________________","--------url---------------"+wxUserInfo.getData()
									.getMemberInfo().getFace()+"--------------------"+wxUserInfo.getData()
									.getMemberInfo().getNickName());
							
							
							// send event
							PersonDataViewEvent event = new  PersonDataViewEvent(
									PersonDataViewEvent.EVENT_UPDATE_USERINFO_VIEW);
							EventBus.getDefault().post(event);
							
						} else {
							Log.e("cuowuxinxi---------","访问服务器出现问题了");
						}

						Log.e(TAG, "success" + "success");
					} catch (Exception e) {
						e.printStackTrace();
					}

				}

				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {

					Log.e(TAG, "failure");
				}

			});

		}
		
		//wei wan cheng 
		public void submitInfo(){
			String url="http://h5.aglhz.com/gc_community/member/center/updateMemberInfo.do";
		}
		
		
		
		
		
}
